require.config({
    baseUrl: './js',
    paths: {
        util: 'component/util',
        listener: 'component/listener',
        baseTrack: 'component/baseTrack',
        baiduTrack: 'component/baiduTrack',
        trackPlayer: 'component/trackPlayer'
    }
});

require(['domReady', "trackPlayer", "baiduTrack", "coordtransform"],
    function(domReady, trackPlay, baiduTrack, coordtransform) {

        domReady(function() {
            //实例化播放器实例
            var player = legendofkage.TrackPlayer();

            setDataSource = function() {
                datasource.ListTL.forEach(function(item, index) {
                    var bdTrack = new legendofkage.BaiduTrack(map, {
                        dataSource: item,
                        autoAdjustViewport: true,
                        speed: 200,
                        calcDistance: true,
                        personMarker: {
                            label: {
                                text: 'testuser'
                            }
                        }
                    });
                    //注册轨迹的单击事件，截获marker的事件
                    bdTrack.addEventListener('click', function(e) {
                        console.log(e);
                        alert(JSON.stringify(e));
                    })
                    player.addTrack(bdTrack);
                });


                var c = coordtransform;
                var tracks = player.getTrack();
                tracks.forEach(function(item) {
                    item.dataAdapter(function(target, from) {
                        var lnglat = coordtransform.bd_mkt2bd_wgs(from.BdX, from.BdY);
                        target.point = new BMap.Point(lnglat[0], lnglat[1]);
                        target.date = from.Date;
                        target.dateString = from.DateString;
                        return target;
                    });
                })
            }

            document.getElementById('cmpSpeed').addEventListener('change', function() {
                player.setPlaySpeed(this.value);
            })

            document.getElementById('userTrack').addEventListener('click', function() {
                if (player.getTrack().length == 0) {
                    setDataSource();
                }
                if (player.getState() != "playing") {
                    player.play();
                } else if (player.getState() == "playing")
                    player.pause();
            })

            document.getElementById('stopBtn').addEventListener('click', function() {
                player.stop();
            })

        });

    });